﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dreamz
{
    public class Rum17
    {
        static public void Apply()
        {
            GameEngine.S.World[2, 6].Items["lever"].OnUse += new EventHandler(UseLever);
            GameEngine.S.World[2, 6].Items.Remove("stone");
        }

        static public void UseLever()
        {
            UI.Console.Clear();
            //Takes input from user and catches up when writing letters or wrong number.
            UI.Console.Write("How many times do you want to pull the lever? ");
            UI.Draw();
            int tries = 0;
            do
            {
                try
                {
                    tries = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    UI.Console.Write("You have to choose a number between 1-20! ");
                    UI.Draw();
                }
            }
            while ((tries < 1) || (tries > 20));

            //Instead of writing combination with words this makes you able to use arrow keys for trying combinations.
            UI.Console.Write("Use arrow keys to select combination of the vault:");
            UI.Draw();
            ConsoleKeyInfo keyPressed = new ConsoleKeyInfo();
            bool right1 = false, right2 = false, right3 = false, right4 = false;
            bool unlocked = false;
            for (int i = 0; i < tries; i++)
            {
                keyPressed = Console.ReadKey(true);
                switch (keyPressed.Key)
                {
                    case ConsoleKey.UpArrow:
                        UI.Console.WriteLine("Up");
                        UI.Draw();
                        break;
                    case ConsoleKey.DownArrow:
                        UI.Console.WriteLine("Down");
                        UI.Draw();
                        break;
                    case ConsoleKey.LeftArrow:
                        UI.Console.WriteLine("Left");
                        UI.Draw();
                        break;
                    case ConsoleKey.RightArrow:
                        UI.Console.WriteLine("Right");
                        UI.Draw();
                        unlocked = RightButton(i, ref right1, ref right2, ref right3, ref right4);
                        break;
                }
                //Code for events to come when combination is solved.
                if (unlocked == true)
                {
                    GameEngine.S.World[2, 6].Description = "You are in the vault room. The vault is open.\n";
                    GameEngine.S.World[2, 6].Items["vault"].Description = "You can se a small tiny thing in the vault. It looks like a stone.";
                    String item = "Stone|Tip of tounge#This oddly shaped stone look like it's been separated from a bigger whole.#canPickUp";
                    GameEngine.S.World[2, 6].Items.Add(item.Split('|')[0].ToLower(), Item.FromString(item));
                    GameEngine.S.World[2, 6].Items["stone"].OnTake += new EventHandler(TakeStone);
                    UI.Console.WriteLine("\nAs you finally move the lever into the 'right' direction,\nthe vault slowly grinds open, dry air rushing out.\nThe whole scene feel surreal, the vault must have stood closed for ages.");
                    break;
                }
            }
            //If you fail opening the vault.
            if (unlocked != true)
            {
                UI.Console.WriteLine("\nYou obviously have forgotten the combination, fool!\n");
            }
        }

        static public bool RightButton(int i, ref bool right1, ref bool right2, ref bool right3, ref bool right4)
        {
            //Contains the code for the opening the vault. Sets bools to true if writing the right combination.
            if (i == 0)
            {
                right1 = true;
            }
            if (i == 1 && right1 == true)
            {
                right2 = true;
            }
            if (i == 2 && right2 == true)
            {
                right3 = true;
            }
            if (i == 3 && right3 == true)
            {
                right4 = true;
            }
            if (i == 4 && right4 == true)
            {
                return true;
            }
            if (i > 4)
            {
                right1 = false;
                right2 = false;
                right3 = false;
                right4 = false;
            }
            return false;
        }
        static public void TakeStone()
        {
            GameEngine.S.Player.Inventory["stone"].OnUse += new EventHandler(TipOfTounge);
            GameEngine.S.World[2, 6].Items["vault"].Description = "The vault is empty.";
        }
        static void TipOfTounge()
        {
            //Code for what will happen if you are in correct room and have the stone. A new door will open up. 
            if (GameEngine.S.Player.Inventory["stone"] != null)
            {
                if (GameEngine.S.Player.CurrentRoom == GameEngine.S.World[3, 3])
                {
                    GameEngine.S.World[3, 3].Description = "As you enter this room, you notice a giant lion-statue, it's mouth wide open.\nThe statue is made out of stone, the tounge vaguely resembling a lever.\n";
                    GameEngine.GameState.leverMovable = true;
                    UI.Console.WriteLine("As you replace the tip of the tounge, the lion suddenly gapes.\n");
                    GameEngine.S.Player.Inventory.Remove("stone");
                }
            }
        }
    }
}